﻿package com.vigmonk.net
{
	import com.vigmonk.events.LoaderEvent;		import flash.display.LoaderInfo;	import flash.display.Sprite;	import flash.events.Event;	
	/**
	 * 
	 * SelfLoader.as
	 * @author VIGMONK
	 * 
	 */
	 
	 /**
	  * 
	  * import com.vigmonk.net.SelfLoader;
	  * 
	  * var selfLoader = new SelfLoader();
	  * selfLoader.addEventListener(SelfLoader.LOAD_COMPLETE,init);
	  * this.addChild(selfLoader);
	  * 
	  * function init(_e:Event):void
	  * {
	  * 	trace('[LOAD COMPLETE]');
	  * }
	  * 
	  */
	  
	public class SelfLoader extends Sprite
	{
		public static const LOAD_PROGRESS:String = 'LOAD_PROGRESS';
		public static const LOAD_COMPLETE:String = 'LOAD_COMPLETE';

		public function SelfLoader():void
		{
			addEventListener(Event.ADDED_TO_STAGE, setEnter);
		}

		private function setEnter(_e:Event):void
		{
			this.addEventListener(Event.ENTER_FRAME, progressHandler);
		}

		private function progressHandler(_e:Event):void
		{
			var loaded:Number 	= this.root.loaderInfo.bytesTotal;
			var total:Number 	= this.root.loaderInfo.bytesLoaded;
			var per:Number 		= loaded / total;
			
			//trace('[SELF LOADER - LOAD_PROGRESS] loaded : ',loaded,'total : ',total);
			dispatchEvent(new LoaderEvent(LOAD_PROGRESS, per));
			
			if(per==1) loadComp();
		}

		private function loadComp():void
		{
			//trace('[SELF LOADER - LOAD_COMPLETE]');
			this.removeEventListener(Event.ENTER_FRAME, progressHandler);
			dispatchEvent(new LoaderEvent(LOAD_COMPLETE));
		}
	}
}